package pers.agony.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.agony.constant.MessageConstant;
import pers.agony.entity.PageResult;
import pers.agony.entity.QueryPageBean;
import pers.agony.entity.Result;
import pers.agony.pojo.CheckGroup;
import pers.agony.pojo.Setmeal;
import pers.agony.service.SetmealService;
import pers.agony.utils.QiNiuUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author : Agony
 * @date : Created in 3:00 下午 2021/11/3
 * @Description:
 * @Modified By:
 * @version: : 1.0
 */
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping("/setmeal/upload.do")
    public Result update(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        String suffix= originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + suffix;
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),filename);
            /**
             * result{
             *     flag:
             *     message:
             *     data:{
             *     	imgName: 唯一的文件名(七牛上的文件名) 补充formData.img
             * 		domain: 七牛上bucket的域名  imageUrl=domain+imgName
             *        }//前端用{}后台用对象 前端用[]后台用list 这里要用对象，只能使用map对象
             * }
             * http://qotintfhc.hn-bkt.clouddn.com/dlrb.jpg
             */
            Map<String, String> Map = new HashMap<>();
            Map.put("imgName",filename);
            Map.put("domain",QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,Map);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @GetMapping("/setmeal/findAll.do")
    public Result findAll(){
        List<Setmeal> list = setmealService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @PostMapping("/setmeal/add.do")
    public Result add(@RequestParam int[] checkgroupIds, @RequestBody Setmeal setmeal){
        setmealService.add(checkgroupIds,setmeal);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @PostMapping("/setmeal/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @GetMapping("/setmeal/findOne.do")
    public Result findOne(@RequestParam int id){
        Setmeal setmeal = setmealService.findOne(id);
        if(setmeal!=null) {
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        }else{
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @GetMapping("/setmeal/findsetmealCheckGroupBysetmealId.do")
    public Result findCheckItemIdsByCheckGroupIds(@RequestParam int id){
        List<Integer> list = setmealService.findsetmealCheckGroupBysetmealId(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
    }

    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @PostMapping("/setmeal/update.do")
    public Result update(@RequestBody Setmeal setmeal, @RequestParam int[] checkgroupIds){
        setmealService.update(setmeal, checkgroupIds);
        return new Result(true,"编辑套餐成功！");
    }

    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @PostMapping("/setmeal/delete.do")
    public Result delete(@RequestParam int id){
        setmealService.delete(id);
        return new Result(true,"套餐删除成功！");
    }
}
